<?php

namespace App\Admin\Controllers;

use App\Models\Campus;
use App\Models\Depot;

use App\Models\Major;
use App\Models\University;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class DepotController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        $user_id=$request->input('user_id');
        return Admin::content(function (Content $content) use ($user_id){

            $content->header('书架');
            $content->description('用户正在使用的书籍');

            $content->body($this->grid($user_id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($user_id)
    {
        return Admin::grid(Depot::class, function (Grid $grid) use ($user_id){
            $grid->disableCreation();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });
            if($user_id){
                $grid->model()->where('user_id', $user_id);
            }
            $grid->id('ID')->sortable();
            $grid->user_id('用户id');
            $grid->university_id('大学')->display(function ($data){
                return University::where('id',$data)->value('name')?:'';

            });
            $grid->campus_id('校区')->display(function ($data){
                return Campus::where('id',$data)->value('name')?:'';

            });
            $grid->total('拥有数量');
            $grid->can_sale_num('正在销售数量');
            $grid->lock_num('交易中数量');


            $grid->goods_status('状态')->display(function ($data){
                //1使用中,2面对面还书中,3等待匹配（等待匹配超过一定的时间限制过后，状态会变更为工作人员取书,时间限制从配置表中读取）,4工作人员取书，5:已流转
                switch ($data){
                    case 0:return '<i style="color:red">停止售卖</i>';
                    case 1:return '<i style="color:green">售卖中</i>';
                    default:return '异常';
                }
            });
            $grid->goods('书籍信息')->display(function($data){
                return "[名称：{$data['title']}][作者：{$data['author']}][出版社：{$data['publisher']}]";
            });
            $grid->created_at('加入时间');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Depot::class, function (Form $form) {

            $form->display('id', 'ID');
            $options=[
                0=>'停止售卖',
                1=>'售卖中',
            ];
            $form->radio('goods_status','书籍状态')->options($options);


        });
    }
}
